using System;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Threading;
namespace Microsoft.Expression.Interactivity.Core
{
	public sealed class TimerTrigger : EventTrigger
	{
		public static readonly DependencyProperty MillisecondsPerTickProperty = DependencyProperty.Register("MillisecondsPerTick", typeof(double), typeof(TimerTrigger), new PropertyMetadata(1000.0));
		public static readonly DependencyProperty TotalTicksProperty = DependencyProperty.Register("TotalTicks", typeof(int), typeof(TimerTrigger), new PropertyMetadata(-1));
		private DispatcherTimer timer;
		private EventArgs eventArgs;
		private int tickCount;
		public double MillisecondsPerTick
		{
			get
			{
				return (double)base.GetValue(TimerTrigger.MillisecondsPerTickProperty);
			}
			set
			{
				base.SetValue(TimerTrigger.MillisecondsPerTickProperty, value);
			}
		}
		public int TotalTicks
		{
			get
			{
				return (int)base.GetValue(TimerTrigger.TotalTicksProperty);
			}
			set
			{
				base.SetValue(TimerTrigger.TotalTicksProperty, value);
			}
		}
		protected override void OnEvent(EventArgs eventArgs)
		{
			this.StopTimer();
			this.eventArgs = eventArgs;
			this.tickCount = 0;
			this.StartTimer();
		}
		protected override void OnDetaching()
		{
			this.StopTimer();
			base.OnDetaching();
		}
		internal void StartTimer()
		{
			this.timer = new DispatcherTimer();
			this.timer.set_Interval(TimeSpan.FromMilliseconds(this.MillisecondsPerTick));
			this.timer.add_Tick(new EventHandler(this.OnTimerTick));
			this.timer.Start();
		}
		internal void StopTimer()
		{
			if (this.timer != null)
			{
				this.timer.Stop();
				this.timer = null;
			}
		}
		private void OnTimerTick(object sender, EventArgs e)
		{
			if (this.TotalTicks > 0 && ++this.tickCount >= this.TotalTicks)
			{
				this.StopTimer();
			}
			base.InvokeActions(this.eventArgs);
		}
	}
}
